package com.jkgh.remedium.objects.messages.in;

import com.jkgh.asin.StateMachine;
import com.jkgh.remedium.RemediumHost;
import com.jkgh.remedium.RemediumServer;
import com.jkgh.remedium.objects.RemediumMethodObject;
import com.jkgh.remedium.objects.messages.RemediumInMessage;
import com.jkgh.remedium.states.Callback;
import com.jkgh.remedium.states.GatheringState;

public class RemediumC2SCaller extends RemediumInMessage {

	public RemediumC2SCaller(RemediumServer server) {
		super(server);
	}

	@Override
	public void consumeContent(final StateMachine machine, final Callback callback) {
		machine.setCurrentState(new GatheringState(machine, 16) {

			@Override
			protected void onNeededBytesAvailable() {
				int hostID = machine.produceInt();
				int objectID = machine.produceInt();
				int methodID = machine.produceInt();
				int callID = machine.produceInt();
				
				RemediumHost host = getServer().getHost(hostID);
				RemediumMethodObject methodObject = host.getMethodObject(objectID);
				methodObject.call(machine, RemediumC2SCaller.this, callID, methodID, callback);
			}
			
		});
	}

}
